package test230610;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/6/10 14:24
 * 1143. 最长公共子序列
 * https://leetcode.cn/problems/longest-common-subsequence/
 */
public class Solution03 {

    public int longestCommonSubsequence(String text1, String text2) {
        int n1 = text1.length();
        int n2 = text2.length();
        char[] s1 = text1.toCharArray();
        char[] s2 = text2.toCharArray();
        // dp[i][j] 表示 text1[0, i] 与 text2[0, j] 中最长公共子序列的长度
        int[][] dp = new int[n1 + 1][n2 + 1];
        for (int i = 1; i <= n1; ++i) {
            for (int j = 1; j <= n2; ++j) {
                if (s1[i - 1] == s2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[n1][n2];
    }
}
